

#include "openssl/crypto.h"
#include "openssl/bio.h"
#include "openssl/rand.h"
#include "openssl/err.h"
#include "cryptlib.h"

static int allow_customize = 1;      /* we provide flexible functions for */
static int allow_customize_debug = 1;/* exchanging memory-related functions at
                                      * run-time, but this must be done
                                      * before any blocks are actually
                                      * allocated; or we'll run into huge
                                      * problems when malloc/free pairs
                                      * don't match etc. */

//static void *(*malloc_func)(size_t)         = malloc;
/*static void *default_malloc_ex(size_t num, const char *file, int line)
	{ return malloc_func(num); }
static void *(*malloc_ex_func)(size_t, const char *file, int line)
        = default_malloc_ex;*/

static void (*malloc_debug_func)(void *,int,const char *,int,int) = NULL;

void *CRYPTO_malloc(int num, const char *file, int line)
{
	void *ret = NULL;
		extern unsigned char cleanse_ctr;

		if (num <= 0) return NULL;

		allow_customize = 0;
		if (malloc_debug_func != NULL)
			{
			allow_customize_debug = 0;
			malloc_debug_func(NULL, num, file, line, 0);
			}
		//ret = malloc_ex_func(num,file,line);
		ret = malloc(num);
	#ifdef LEVITTE_DEBUG_MEM
		fprintf(stderr, "LEVITTE_DEBUG_MEM:         > 0x%p (%d)\n", ret, num);
	#endif
		if (malloc_debug_func != NULL)
			malloc_debug_func(ret, num, file, line, 1);

	        /* Create a dependency on the value of 'cleanse_ctr' so our memory
	         * sanitisation function can't be optimised out. NB: We only do
	         * this for >2Kb so the overhead doesn't bother us. */
	        if(ret && (num > 2048))
	                ((unsigned char *)ret)[0] = cleanse_ctr;

		return ret;
}

//static void (*free_func)(void *)            = free;
static void (*free_debug_func)(void *,int) = NULL;

void CRYPTO_free(void * str)
{
	if (free_debug_func != NULL)
			free_debug_func(str, 0);
	#ifdef LEVITTE_DEBUG_MEM
		fprintf(stderr, "LEVITTE_DEBUG_MEM:         < 0x%p\n", str);
	#endif
		free(str);
		if (free_debug_func != NULL)
			free_debug_func(NULL, 1);
}

static void (*realloc_debug_func)(void *,void *,int,const char *,int,int) = NULL;

void *CRYPTO_realloc(void *str, int num, const char *file, int line)
	{
	void *ret = NULL;

	if (str == NULL)
		return CRYPTO_malloc(num, file, line);

	if (num <= 0) return NULL;

	if (realloc_debug_func != NULL)
		realloc_debug_func(str, NULL, num, file, line, 0);
	//ret = realloc_ex_func(str,num,file,line);
	ret = realloc(str, num);
#ifdef LEVITTE_DEBUG_MEM
	fprintf(stderr, "LEVITTE_DEBUG_MEM:         | 0x%p -> 0x%p (%d)\n", str, ret, num);
#endif
	if (realloc_debug_func != NULL)
		realloc_debug_func(str, ret, num, file, line, 1);

	return ret;
	}

static int  (*push_info_func)(const char *info, const char *file, int line)
	= NULL;
static int  (*pop_info_func)(void) = NULL;

int CRYPTO_push_info_(const char *info, const char *file, int line)
	{
	if (push_info_func)
		return push_info_func(info, file, line);
	return 1;
	}

int CRYPTO_pop_info(void)
	{
	if (pop_info_func)
		return pop_info_func();
	return 1;
	}
/*
void OPENSSL_cleanse(void *ptr, size_t len)
{
	
}

void CRYPTO_lock(int mode, int type,const char *file,int line)
{
	
}

int	BIO_write(BIO *b, const void *data, int len)
{
	
}

BIO_METHOD *BIO_s_file(void )
{
	
}

BIO *	BIO_new(BIO_METHOD *type)
{
	
}

int	BIO_free(BIO *a)
{
	
}

long	BIO_ctrl(BIO *bp,int cmd,long larg,void *parg)
{
	
}


int BIO_snprintf(char *buf, size_t n, const char *format, ...)
	//__bio_h__attr__((__format__(__printf__,3,4)))
{
	
}

int  RAND_pseudo_bytes(unsigned char *buf,int num)
	{
	
	}

int  RAND_bytes(unsigned char *buf,int num)
	{
	
	
	}

void RAND_add(const void *buf,int num,double entropy)
	{
	
	}

unsigned long ERR_peek_last_error(void)
	{
	
	}

void ERR_load_strings(int lib,ERR_STRING_DATA str[])
	{
	
	}

const char *ERR_func_error_string(unsigned long e)
	{
	
	}

void ERR_clear_error(void )
	{
	
	}
*/
